//
//  MenuBar.swift
//  Youtube2
//
//  Created by 劢克科技 on 2019/5/6.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit

class MenuBar: UIView {
    
    let barCellId = "barCellId"
    let images = ["home", "fire", "file", "myon"]

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()

        // 在数据加载完毕后调用
        barContainer.layoutIfNeeded()
        barContainer.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: .centeredHorizontally)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - Getter
    
    lazy var barContainer: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.register(MenuCell.self, forCellWithReuseIdentifier: self.barCellId)
        cv.delegate = self
        cv.dataSource = self
        cv.backgroundColor = UIView.rgb(red: 230, green: 32, blue: 32)
        return cv
    }()
}

//MARK: - <Collection Delegate/DataSource>

extension MenuBar: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: barCellId, for: indexPath) as! MenuCell
        cell.imageView.image = UIImage(named: images[indexPath.row])?.withRenderingMode(.alwaysTemplate)
        cell.tintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: bounds.width / 4, height: bounds.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
}

//MARK: - UI

extension MenuBar {
    fileprivate func setUI() {
        addSubview(barContainer)
        barContainer.frame = self.bounds
    }
}

//MARK: - Cell Class

class MenuCell: UICollectionViewCell {
    
    override var isSelected: Bool {
        didSet {
            tintColor = self.isSelected ? UIColor.white : #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        }
    }
    
    override var isHighlighted: Bool {
        didSet {
            tintColor = self.isHighlighted ? UIColor.white : #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        addSubview(imageView)
        imageView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
        imageView.center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
    }
    
    //MARK: - Getter
    
    lazy var imageView: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        return image
    }()
}
